/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import core.SellerCore;
import core.StepOwnerCore;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Costumer;
import model.Product;
import model.Seller;

/**
 *
 * @author phinguyen
 */
public class ServletLaunchDap extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        RequestDispatcher rd;
        try  {
            SellerCore sellcore = new SellerCore();
            String customerName = request.getParameter("customer");
            String title = request.getParameter("title");
            
            //récupérer identifiant du vendeur pour ici !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            Integer sellerID = 1;
            HttpSession session= request.getSession();
            Seller seller=(Seller) session.getAttribute("user");
            
            String product1name = request.getParameter("product1");
            String product2name = request.getParameter("product2");
            String product3name = request.getParameter("product3");
            String product4name = request.getParameter("product4");
            HashMap<Product,Integer> hash = new HashMap<Product,Integer>();
            
            if(!product1name.equals("--")){
            Product product1 = sellcore.getProductbyName(request.getParameter("product1"));
            Integer qty1 = new Integer(request.getParameter("quantity1"));
            hash.put(product1, qty1);
        }
            if (!product2name.equals("--")){
            Product product2 = sellcore.getProductbyName(request.getParameter("product2"));
            Integer qty2 = new Integer(request.getParameter("quantity2"));
            hash.put(product2, qty2);
        }
            if (!product3name.equals("--")){
            Product product3 = sellcore.getProductbyName(request.getParameter("product3"));
            Integer qty3 = new Integer(request.getParameter("quantity3"));
            hash.put(product3, qty3);
        }
            if (!product4name.equals("--")){
            Product product4 = sellcore.getProductbyName(request.getParameter("product4"));
            Integer qty4 = new Integer(request.getParameter("quantity4"));
            hash.put(product4, qty4);
        }
           
            Costumer customer = sellcore.getCostumerbyName(customerName);
//            Seller seller = sellcore.getSellerbyID(sellerID);
            sellcore.createDAP(hash, customer, title, seller);
            rd = request.getRequestDispatcher("sellerConfirLaunch.jsp");
            rd.forward(request, response);
        } finally {
            out.close();
        }
        }
    
  // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
